package com.sanri.test.testmybatis;

import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class MapperParserMain {
    @Test
    public void test() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("BatchMapper.xml");
        InputStream inputStream = classPathResource.getInputStream();
        String resource = classPathResource.getFilename();
        Configuration configuration = new Configuration();
        XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,new HashMap<>());
        mapperParser.parse();

        MappedStatement mappedStatement = configuration.getMappedStatement("com.sanri.test.testmybatis.mapper.BatchMapper.selectByName");
        Map<String,Object> param = new HashMap<>();
        param.put("ename", "abc");
        BoundSql boundSql = mappedStatement.getBoundSql(param);
        String sql = boundSql.getSql();
        System.out.println(mappedStatement.getBoundSql("dasfasdf").getSql());
        System.out.println(sql);
    }
}
